package sample;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class Main extends Application {
    /** 定义偏移量，用于处理窗口移动 */
    private double xOffset = 0;
    private double yOffset = 0;

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Bilisample.fxml"));
        int width = 640;
        int height = 313;
        Rectangle rec = new Rectangle(width, height);
        rec.setArcWidth(30);
        rec.setArcHeight(30);
        root.setClip(rec);
        // 在窗口内拖拽鼠标实现窗口移动
        root.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            /*
             * 根据偏移量设置primaryStage新的位置
             */
            @Override
            public void handle(MouseEvent event) {
                primaryStage.setX(event.getScreenX() - xOffset);
                primaryStage.setY(event.getScreenY() - yOffset);
            }
        });
        // 设置标题
        primaryStage.setTitle("BiliBili Fans");
        primaryStage.setScene(new Scene(root, width, height));
        // 将主窗口设置为无边框
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
